/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    2010 year
 *    Author: Michael Khromov aka MikeDM (Mike D'Morto)
 *    E-Mail: mikedmorto@gmail.com
 */

#include "mconfig.h"
#include "ui_mconfig.h"
#include <QString>
#include <QFileDialog>
#include "QDebug"

Mconfig::Mconfig(Muconfig * cfg,QList<MutesterPluginInterface*> * pl, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Mconfig)
{
    ui->setupUi(this);
    this->mpl = pl;
    this->plid=cfg->plid;
    connect(this->ui->btn_getpath,SIGNAL(clicked()),SLOT(setGetpath()));
    connect(this->ui->btn_putpath,SIGNAL(clicked()),SLOT(setPutpath()));
    // показ конфигурационного диалога плагина
    connect(this->ui->btn_plugin_setup,SIGNAL(clicked()),SLOT(slotDia()));
    // ставим ид текущего плагина
    connect(this->ui->cb_pluglist,SIGNAL(activated(int)),SLOT(slotSetCurrPlId(int)));
    this->ui->le_name->setText(cfg->name);
    this->ui->le_get->setText(cfg->get);
    this->ui->le_put->setText(cfg->put);
    this->ui->sb_timer->setValue(cfg->timer);
    this->ui->sb_eiget->setValue(cfg->eiget);
    this->ui->sb_eiput->setValue(cfg->eiput);
    // заполняем листинг доступных плагинов
    // первым идет дефолтный (внутренний)
        this->ui->cb_pluglist->addItem("None");
    for(int i = 0; i < this->mpl->size(); i++){
        this->ui->cb_pluglist->addItem(this->mpl->at(i)->getPluginName());
    }
    this->ui->cb_pluglist->setCurrentIndex(this->plid+1); // устанавливаем номер текущего плагина.
    if(this->ui->cb_pluglist->currentIndex()==0){
        // блокируем кнопку настройки плагина если дефолтный плагин.
        this->ui->btn_plugin_setup->setEnabled(false);
    }

    qDebug()<<"Test current plugin id "<<this->plid;

}

Mconfig::~Mconfig()
{
    delete ui;
}

void Mconfig::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}


void Mconfig::getConfig(Muconfig *cfg){
    cfg->name=this->ui->le_name->text();
    cfg->get=this->ui->le_get->text();
    cfg->put=this->ui->le_put->text();
    cfg->timer=this->ui->sb_timer->value();
    cfg->eiget=this->ui->sb_eiget->value();
    cfg->eiput=this->ui->sb_eiput->value();
    cfg->plid=this->plid;

    //cfg->currentPlugin=this->mpl->at(this->plid)->getPluginName();

}


void Mconfig::setGetpath(){
    QFileDialog fd;
    fd.setFileMode(QFileDialog::ExistingFile);
    this->ui->le_get->setText(fd.getOpenFileName());
}

void Mconfig::setPutpath(){
    QFileDialog fd;
    fd.setFileMode(QFileDialog::ExistingFile);
    this->ui->le_put->setText(fd.getOpenFileName());
}

void Mconfig::slotDia(){
    int currentId=this->ui->cb_pluglist->currentIndex()-1;
    if(currentId>=0 and currentId < this->mpl->size()){
        this->mpl->at(currentId)->execDia();
    }

}

void Mconfig::slotSetCurrPlId(int id){
    qDebug()<<"Current ID ="<<id;
    // устанавливаем текущий плагин по иду
    if(this->ui->cb_pluglist->currentIndex()==0){
        this->ui->btn_plugin_setup->setEnabled(false);
    }else{
        this->ui->btn_plugin_setup->setEnabled(true);
    }
    this->plid=id-1;
}
